Cyan.importJs("event.js");
Cyan.customEvents.add("active");
Cyan.customEvents.add("passivate");
Cyan.Div = {
    zIndex:10000,
    divs:[],
    tips:[],
    active:function(div)
    {
        if (!div)
            div = this;
        Cyan.Array.add(Cyan.Div.divs, div);
        if (div.style.display == "none")
            div.style.display = "block";
        div.style.zIndex = Cyan.Div.zIndex++;
        Cyan.each(Cyan.Div.divs, function()
        {
            Cyan.Event.invoke(this, this == div ? "active" : "passivate");
        });
    },
    showModal:function(div)
    {
        if (!div)
            div = this;
        if (!div.modalDiv)
        {
            div.modalDiv = document.createElement("DIV");
            div.modalDiv.style.position = "absolute";
            div.modalDiv.style.background = "ghostwhite";
            if (Cyan.navigator.isIE())
                div.modalDiv.style.filter = "alpha(opacity=35)";
            else
                div.modalDiv.style.opacity = "0.35";
            div.modalDiv.style.left = "0";
            div.modalDiv.style.top = "0";
            document.body.appendChild(div.modalDiv);
        }
        div.modalDiv.style.width = document.body.clientWidth;
        div.modalDiv.style.height = document.body.clientHeight;
        div.modalDiv.style.zIndex = Cyan.Div.zIndex++;
        div.modalDiv.style.display = "";
        Cyan.Div.active(div);
    },
    closeModal:function(div)
    {
        if (!div)
            div = this;
        if (div.modalDiv)
            div.modalDiv.style.display = "none";
    }
};
Cyan.Tip = function(destoryWhenClose)
{
    this.destoryWhenClose = destoryWhenClose;
};
Cyan.Tip.prototype.show = function(x, y)
{
    var div = this;
    window.setTimeout(function()
    {
        div.show0(x, y);
    }, 50);
};
Cyan.Tip.prototype.show0 = function(x, y)
{
    if (!this.div)
    {
        this.div = this.createDiv();
        this.div.style.position = "absolute";
        document.body.appendChild(this.div);
    }
    this.div.style.left = x + "px";
    this.div.style.top = y + "px";
    this.div.style.display = "";
    var b = false;
    var length = Cyan.Div.tips.length;
    for (var i = 0; i < length; i++)
    {
        if (Cyan.Div.tips[i] != this)
        {
            Cyan.Div.tips[i].close();
            if (Cyan.Div.tips.length != length)
            {
                length--;
                i--;
            }
        }
        else
        {
            b = true;
        }
    }
    if (!b)
        Cyan.Div.tips.push(this);
    Cyan.Div.currentTip = this;
};
Cyan.Tip.prototype.hide = function()
{
    if (this.div)
        this.div.style.display = "none";
    if (Cyan.Div.currentTip == this)
        Cyan.Div.currentTip = null;
};
Cyan.Tip.prototype.destory = function()
{
    if (this.div)
    {
        this.div = null;
        document.body.removeChild(this.div);
        Cyan.Array.removeElement(Cyan.Div.tips, this);
    }
    if (Cyan.Div.currentTip == this)
        Cyan.Div.currentTip = null;
};
Cyan.Tip.prototype.close = function()
{
    if (this.destoryWhenClose)
        this.destory();
    else
        this.hide();
};
Cyan.Tip.prototype.getPosition = function(component)
{
    var p = Cyan.Elements.getPosition(component);
    return {x:p.x,y:p.y + component.clientHeight};
};
Cyan.Tip.prototype.showOnComponent = function(component)
{
    component = Cyan.$(component);
    var p = this.getPosition(component);
    this.show(p.x, p.y);
};
Cyan.onload(function()
{
    Cyan.attach(window, "resize", function()
    {
        Cyan.each(Cyan.Div.divs, function()
        {
            if (this.modalDiv)
            {
                this.modalDiv.style.width = document.body.clientWidth;
                this.modalDiv.style.height = document.body.clientHeight;
            }
        });
    });
    Cyan.attach(document, "click", function(event)
    {
        if (Cyan.Div.currentTip)
        {
            if (!event.isOn(Cyan.Div.currentTip.div))
                Cyan.Div.currentTip.close();
        }
    });
});
Cyan.Elements.prototype.active = function()
{
    this.each(Cyan.Div.active);
    return this;
};
Cyan.Elements.prototype.showModal = function()
{
    this.each(Cyan.Div.showModal);
    return this;
};
Cyan.Elements.prototype.closeModal = function()
{
    this.each(Cyan.Div.closeModal);
    return this;
};